jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.topServiceHits.TopServiceHits", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	plugin.prototype.screenId;
			plugin.prototype.nameSuffix = "";

            plugin.prototype.init = function() {
                this.jsxsuper();
            }

            plugin.prototype.setUpComponent = function(nameSuffix, screenId) {
            	this.screenId = screenId;
            	this.nameSuffix = nameSuffix;
				var tableLabel = this.getDescendantOfName("topHitsTableLabel");
				tableLabel.setName(tableLabel.getName() + nameSuffix);
				var topServiceHitsTable = this.getDescendantOfName("topServiceHitsTable");
				topServiceHitsTable.setName(topServiceHitsTable.getName() + nameSuffix);
				
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.selectedTimeframeSubject, this.timeframeSelected, this);
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.environmentChangeSubject, this.environmentSelected, this);
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, this.handleDisplayScreen, this);
	            this.callgetTopServiceHits();
            }

            plugin.prototype.timeframeSelected = function(subject, message, tableObj) {
            	var timeframeId = message.selectedTimeframe;
            	tableObj.callgetTopServiceHits(timeframeId, null);
            }
            
            plugin.prototype.environmentSelected = function(subject, message, tableObj) {
            	var envId = message.environmentId;
            	tableObj.callgetTopServiceHits(null, envId);
            }

            plugin.prototype.getNamespace = function() {
				return "tibco.admin";
			}
			
			plugin.prototype.handleDisplayScreen = function(subject, message, tableObj) {
            	if(message.screenId == tableObj.screenId)
            	{ tableObj.updateComponent(); }
            }
			
			plugin.prototype.updateComponent = function() {
				this.callgetTopServiceHits();
			}

			plugin.prototype.callgetTopServiceHits = function(timeframeId, environmentId) {
				var server = tibco.admin.Main.getServer();

				if (server.getEnv("MODE")) {
					// mode is live
					var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/topServiceHits/rules/topServiceHitsTableRules.xml", "getTopServiceHits");
					objService.setEndpointURL(plugin.wsEndpointURL);
					// this setInboundURL should not be required...only here because of a current AMX framework bug
					objService.setInboundURL("tibco/admin/monitormanage/uicomponents/table/topServiceHits/data/topServiceHitsTableData.xml");
				}
				else {
					var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/topServiceHits/rules/topServiceHitsTableRules.xml"), "getTopServiceHits", null, null);
					// mode is static
					objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/topServiceHits/data/topServiceHitsTableData.xml"));
				}
				
				objService.topServiceHitsObj = this;
				if(!timeframeId)
				{ timeframeId = tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeId(); }
				if(!environmentId)
				{ environmentId = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.getCurrentEnvironmentId(); }
				objService.timeframeId = timeframeId;
				objService.environmentId = environmentId;

				//subscribe and call
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetTopServiceHitsSuccess);
				objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetTopServiceHitsError);
				objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetTopServiceHitsInvalid);
				objService.doCall();
		    };

			plugin.prototype.ongetTopServiceHitsSuccess = function(objEvent) {
				var topServiceHitsObj = objEvent.target.topServiceHitsObj;
				var tableObj = topServiceHitsObj.getDescendantOfName("topServiceHitsTable" + topServiceHitsObj.nameSuffix);
				tableObj.repaintData();
				//var responseXML = objEvent.target.getInboundDocument();
				//alert(responseXML);
				//jsx3.log("Success","The service call was successful.");
      				//var mappedData = tibco.admin.Main.getServer().Cache.getDocument("topServiceFaultsData");
		    };

			plugin.prototype.ongetTopServiceHitsError = function(objEvent) {
				var myStatus = objEvent.target.getRequest().getStatus();
				jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
		    };

			plugin.prototype.ongetTopServiceHitsInvalid = function(objEvent) {
				jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
		    };
			
			plugin.showServiceDetail = function(serviceIdVal) {
        		var paramArray = { serviceId:serviceIdVal };
        		tibco.admin.monitormanage.general.NavigationHandler.publishMainPageNavigationMessage(tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceScreenId, paramArray); 
        	}

        });